<template>
  <div class="content">
    <nut-navbar class="navbar" z-index="5" :clickable="false" fixed safe-area-inset-top>
      <template #content>
        <span>{{ t(String(route.meta.title)) }}</span>
      </template>
      <template #left>
        <img src="/imgs/club/arrow_left.png" alt="" @click="router.go(-1)" class="arrow_left" />
      </template>
      <template #right>
        <!-- <span @click="onSubmitCreate" class="save">保存</span> -->
      </template>
    </nut-navbar>
    <div class="top_upload">
      <nut-uploader
          accept=".jpg,.jpeg,.png,.gif,.webp"
          :before-xhr-upload="beforeUpload"
          @delete="(e) => {removeImg(e)}"
      >
      </nut-uploader>
      <p>{{t('上传俱乐部图片')}}</p>
      <p>限JPG,JPEG,PNG格式,10MB以内</p>
    </div>
    <div class="form_box">
      <nut-form label-position="top" ref="formRef" >
        <nut-form-item label="俱乐部名称">
          <nut-input v-model.trim="createParams.name" limit-show :max-length="40" :placeholder="t('请输入俱乐部名称')" clearable />
        </nut-form-item>
        <nut-form-item label="俱乐部简介" class="dec">
          <nut-textarea :rows="3" limit-show :max-length="200" v-model.trim="createParams.dec" :placeholder="t('请输入俱乐部简介')" clearable />
        </nut-form-item>
      </nut-form>
    </div>

    <div class="bottom_btn">
      <Btn height="44px" width="320px" class="submit" @click="submitAc">提交</Btn>
    </div>
  </div>
</template>

<script setup lang="ts">
import { useI18n } from "vue-i18n";
import { ref } from "vue";
import { useRoute, useRouter } from "vue-router";
import {showNotify} from "@nutui/nutui";
import {storeToRefs} from "pinia";
import {User} from "@/store/user.ts";
import pinia from "@/store";
import Btn from "@/components/Btn.vue";

const route = useRoute();
const router = useRouter();
const UserStore = User(pinia);
const { roleInfo } = storeToRefs(UserStore);

const { t } = useI18n();


// 创建俱乐部表单提交
const createParams = ref({
  name: '',
  dec: '',
});

// 上传图片
const beforeUpload = (xhr: any, data: any) => {
  console.log(xhr)
  const file = data.sourceFile

  if (file && file.size > 1024 * 1024 * 2) { // 2MB限制
    showNotify.danger('文件大小不能超过2MB！')
    return;
  }
  const formData = new FormData();
  formData.append('avatar', file);
  formData.append('role_id', `${roleInfo.value.id}`);
  fetch(`http://18.162.112.52:8031/api/upload/avatar`, {
    method: 'POST',
    body: formData,
  }).then(response => response.json()).then(response => {
    if (response.status == 200 || response.status == 'success') {
      const urlImg = response.data.path
      showNotify.success(response.message)
      console.log('&&&&&', urlImg)
    } else {
      showNotify.danger(response.message)
    }
  })
}
const removeImg = (e: any) => {
  console.log('删除---', e)
}

const submitAc = () => {
  console.log('加入--')
  router.push('/club/detail')
}

</script>

<style scoped lang="scss">
.content {
  font-size: 14px;
  margin: 0 0 10px;
  color: #fff;

  .arrow_left {
    width: 24px;
    height: 24px;
  }

  .top_upload {
    padding: 15% 0 0;
    p {
      font-size: 15px;
      color: rgba(192, 194, 219, 1);
      text-align: center;
      margin-top: 10px;
    }
    :deep(.nut-uploader) {
      margin: 0 auto;
      width: 120px;
      height: 120px;
      background: rgba(26, 12, 66, 1);
      border: 1px solid rgba(50, 44, 89, 1);
      border-radius: 50%;
      display: flex;
      justify-content: center;
      align-items: center;

      .nut-uploader__upload {
        background: url(/imgs/club/add_ren.png) center no-repeat;
        background-size: 70px;
        border-radius: 50%;

        &::before {
          content: '';
          position: absolute;
          width: 32px;
          height: 32px;
          border-radius: 50%;
          bottom: 0;
          right: 6px;
          background: radial-gradient(100% 103.23% at 50% 0%, #1170FF 0%, #1154FF 61.51%, #6B11FF 98.5%);
        }
        svg {
          width: 24px;
          height: 24px;
          color: #fff !important;
          position: absolute;
          bottom: 4px;
          right: 10px;
        }
      }
      .nut-uploader__upload, .nut-uploader__preview, .nut-uploader__preview-img {
        width: 100%;
        height: 100%;
        margin-right: 0;
        .close {
          color: #fff;
        }
      }
      .nut-uploader__preview__progress {
        display: none;
      }
    }
  }

  .form_box {
    width: 347px;
    height: 176px;
    margin: 10px auto 0;
    padding: 10px;
    border-radius: 8px;
    // background: #100E26;
    // box-shadow: 0.5px 0.5px 1px 0px #2E296B inset;
    form {
      .add_title {
        font-size: 14px;
        color: #fff;
        margin-bottom: 10px;
      }

      :deep(.nut-cell-group__wrap) {
        box-shadow: none;
        background: none;
        margin: 0;

        .nut-cell {
          background: none;
          padding: 0;
          margin-bottom: 14px;

          &::after {
            border: none;
          }

          &.nut-form-item {

            .nut-form-item__label {
              color: #fff;
            }
            .nut-input,
            .nut-textarea {
              color: #fff;
              width: 100%;
              background: #1D1C3C;
              box-shadow: 0.5px 0.5px 1px 0px #333260 inset;
              border: 1px solid #1D1C3C;
              border-radius: 8px;
              padding: 10px !important;
              .nut-textarea__textarea {
                color: #fff;
                height: 175px;
              }
            }
          }
        }
      }

    }
  }

  .bottom_btn {
    position: fixed;
    bottom: 56px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 99;
  }
}
</style>
